var t = require;
var e = module;
var o = exports;
var a,
    i =
        (this && this.__extends) ||
        ((a = function (t, e) {
            return (a =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (t, e) {
                        t.__proto__ = e;
                    }) ||
                function (t, e) {
                    for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o]);
                })(t, e);
        }),
        function (t, e) {
            a(t, e);
            function o() {
                this.constructor = t;
            }
            t.prototype = null === e ? Object.create(e) : ((o.prototype = e.prototype), new o());
        });
Object.defineProperty(o, "__esModule", {value: !0});
var n = (function (t) {
        i(e, t);
        function e(e, o) {
            void 0 === o && (o = !1);
            var a = t.call(this, e, o) || this;
            a.dataVale = "";
            a.paydata = "";
            a.guild = "";
            if (o) {
                a.loadData();
                a.init();
                a.setStoreTime(e, 100);
            }
            return a;
        }
        e.prototype.init = function () {};
        return e;
    })(t("BaseManager").ArchiveBase),
    r = new ((function () {
        function t() {
            this.saveKey = "PlayerDataSave";
            this.canLoadData = !1;
            this.sendArr = [];
            this.saveObj1 = {};
            this.saveObj2 = {};
            this.saveObj3 = {};
            this.checkOverCount = 0;
            this.saveKeyData = {0: "paydata", 1: "guild", 2: "dataVale"};
            this.timeData = null;
        }
        t.prototype.initAccount = function (t) {
            var e = this.saveKey,
                o = cc.sys.localStorage.getItem(e);
            this.saveKey = "PlayerData_" + t;
            if (o) {
                cc.sys.localStorage.setItem(this.saveKey, o);
                cc.sys.localStorage.setItem(e, "");
            }
            this._archive = new n(this.saveKey, !0);
            this.synGetClouds();
            this.checkOverCount = 0;
        };
        t.prototype.getSaveData = function () {
            var t = this;
            null != this.timeData && clearTimeout(this.timeData);
            this.timeData = setTimeout(function () {
                t.startGetSave();
                t.timeData = null;
            }, 200);
        };
        t.prototype.startGetSave = function () {
            this.sendArr.length = 0;
            var t = PayData.getPlayerData();
            if ("" != t && this._archive.paydata != t) {
                this._archive.paydata = t;
                this.saveObj1 = this.setSaveObj(this.saveObj1, this.saveKeyData[0], t);
                this.sendArr.push(this.saveObj1);
                this._archive.saveData(this.saveKey, this._archive, !1, !1, !0);
                console.log("PlayerData -----------------getSaveDat支付1");
            }
            var e = GuildCache.getPlayerData();
            if ("" != e && this._archive.guild != e) {
                this._archive.guild = e;
                this.saveObj2 = this.setSaveObj(this.saveObj2, this.saveKeyData[1], e);
                this.sendArr.push(this.saveObj2);
                this._archive.saveData(this.saveKey, this._archive, !1, !1, !0);
                console.log("PlayerData -----------------getSaveData 引导2");
            }
            var o = GameDataValue.getPlayerData();
            if ("" != o && this._archive.dataVale != o) {
                this._archive.dataVale = o;
                this.saveObj3 = this.setSaveObj(this.saveObj3, this.saveKeyData[2], o);
                this.sendArr.push(this.saveObj3);
                this._archive.saveData(this.saveKey, this._archive, !1, !1, !0);
                console.log("PlayerData -----------------getSaveData 玩家信息3");
            }
            this.synToClouds(this.sendArr);
        };
        t.prototype.canEnterGame = function () {
            return 3 == this.checkOverCount;
        };
        t.prototype.setSaveObj = function (t, e, o) {
            t.key = e;
            t.data1 = o;
            t.data2 = "";
            t.deadline = Date.now();
            return t;
        };
        t.prototype.getParseGuildString = function () {
            return this._archive.guild;
        };
        t.prototype.getParseDataValueString = function () {
            return this._archive.dataVale;
        };
        t.prototype.getParsePayString = function () {
            return this._archive.paydata;
        };
        t.prototype.synGetClouds = function (t) {
            var e = this;
            void 0 === t && (t = 5);
            if (t <= 0) this.canLoadData = !0;
            else if (!this.canLoadData)
                if (GameCof.UID && GameCof.token) {
                    this.canLoadData = !0;
                    var o = GameCof.getCacheCloudData();
                    if (o) {
                        var a = this._archive;
                        for (var i in this.saveKeyData) {
                            var n = this.saveKeyData[i];
                            o[n] && (a[n] = o[n]);
                        }
                        a.saveData();
                    }
                } else
                    setTimeout(function () {
                        e.synGetClouds(t - 1);
                    }, 1e3);
        };
        t.prototype.synToClouds = function (t, e, o, a) {
            void 0 === e && (e = !1);
            if (GameCof.canSynToClouds && GameCof.UID && GameCof.token && !(t.length <= 0)) {
                for (var i = [], n = 0; n < t.length; n++) i.push(t[n]);
                var r = GameCof.getLoginDataVersion(),
                    s = {
                        uniqueID: GameCof.getSaveId(),
                        token: GameCof.token,
                        dataAry: i,
                        isClearData: e,
                        gameid: GameCof.gameid,
                        channel: GameCof.channel,
                        storage: r
                    };
                console.log("PlayerData ----------------- params", s);
                var c = GameCof.baseUrlV3 + GameCof.baseLibPathV3 + GameCof.pathCloudsStorage;
                this.requestJson(
                    c,
                    function (t) {
                        console.log("同步", JSON.stringify(t));
                        if (0 != t.code) {
                            o && o();
                            console.log("同同步结束,数量:", i.length);
                        } else console.log("同步失败:", t.msg);
                    },
                    this,
                    "POST",
                    function (t) {
                        console.log("同步", "请求失败", JSON.stringify(t));
                        a && a();
                    },
                    s,
                    "application/json"
                );
            }
        };
        t.prototype.requestJson = function (t, e, o, a, i, n, r) {
            void 0 === a && (a = "GET");
            void 0 === i && (i = null);
            void 0 === n && (n = null);
            void 0 === r && (r = "application/x-www-form-urlencoded");
            var s = new XMLHttpRequest();
            s.onreadystatechange = function () {
                if (4 == s.readyState)
                    if (s.status >= 200 && s.status < 400) e && e.call(o, JSON.parse(s.responseText));
                    else {
                        console.log("XMLHttpRequest error:", t, s.status);
                        i && i.call(o, s);
                    }
            };
            s.open(a, t, !0);
            s.setRequestHeader("Content-type", r);
            null == n ? s.send() : s.send(JSON.stringify(n));
        };
        return t;
    })())();
window.PlayerData = r;
o.default = r;
